package com.naiterui.ehp.bs.recommend.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bp.bo.doctor.RecomDoctorBO;
import com.naiterui.ehp.bp.bo.doctor.RecomUserInfoBO;
import com.naiterui.ehp.bp.bo.medication.MedicationDetailBO;
import com.naiterui.ehp.bp.bo.medication.param.MedicationParamBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.recommend.bean.DrStandardDiagnosis;
import com.naiterui.ehp.bs.recommend.exception.ExceptionCodes;
import com.naiterui.ehp.bs.recommend.remote.IRecommendRemoter;
import com.naiterui.ehp.bs.recommend.repository.mysql.IDrStandardDiagnosisRepository;
import com.naiterui.ehp.bs.recommend.service.IRecomDraftService;
import com.naiterui.ehp.bs.recommend.vo.MedicationBoxVO;
import com.naiterui.ehp.bs.recommend.vo.draft.DraftBoxItemVO;
import com.naiterui.ehp.bs.recommend.vo.draft.DraftBoxParamVO;
import com.naiterui.ehp.bs.recommend.vo.draft.DraftBoxVO;

@Service
public class RecomDraftServiceImpl implements IRecomDraftService {

    private static Logger LOGGER = LoggerFactory.getLogger(RecomDraftServiceImpl.class);


    private static String RECOM_DELETE_DIAGNOSE_TIP = ConfigUtil.getString("recom.delete.diagnose.tip");


    @Autowired
    private IRecommendRemoter remoter;
    @Autowired
    private IDrStandardDiagnosisRepository drStandardDiagnosisRepository;

    /**
     * 获取草稿箱信息
     *
     * @param doctorId
     * @param paramVO
     *
     * @return
     *
     * @throws BusinessException
     * @since 3.5.0
     */

    @Override
    public DraftBoxVO getDraftInfo(Long doctorId, DraftBoxParamVO draftBoxParamVO) throws BusinessException {

        DraftBoxVO draftBoxVO = new DraftBoxVO();
        //批量获取药品安全用量
        if (CollectionUtils.isNotEmpty(draftBoxParamVO.getPids())) {
            //获取药品列表
            MedicationParamBO medicationParamBO = new MedicationParamBO();
            medicationParamBO.setDoctorId(doctorId);
            medicationParamBO.setPids(draftBoxParamVO.getPids());
            medicationParamBO.setRecommend(false);
            // 缓存药品信息
            Map<Long, MedicationBoxVO> result = new HashMap<>();
            List<MedicationDetailBO> medicationDetailBOList = this.remoter.getMedicationDetailList(medicationParamBO);
            List<MedicationBoxVO> listMedicationBox = new ArrayList<>();
            BeanUtil.copyList(medicationDetailBOList, listMedicationBox, MedicationBoxVO.class);
            for (MedicationBoxVO medicationBoxVO : listMedicationBox) {
                result.put(medicationBoxVO.getId(), medicationBoxVO);
            }
            List<DraftBoxItemVO> itemList = new ArrayList<>();
            for (Long pid : draftBoxParamVO.getPids()) {
                MedicationBoxVO medicationBoxVO = result.get(pid);
                DraftBoxItemVO draftBoxItem = null;
                if (medicationBoxVO != null) {
                    draftBoxItem = new DraftBoxItemVO(medicationBoxVO.getId(), medicationBoxVO.getSkuId(), medicationBoxVO.getName(),
                            medicationBoxVO.getCommonName(),
                            medicationBoxVO.getRecomName(),
                            medicationBoxVO.getSalePrice());

                } else {
                    draftBoxItem = new DraftBoxItemVO();
                    draftBoxItem.setPid(pid);
                }
                itemList.add(draftBoxItem);
            }
            draftBoxVO.setItemList(itemList);
        }

        //表示医生是灰度诊断医生,删除非标准诊断
        List<String> diagnosis = draftBoxParamVO.getDiagnosis();
        if (CollectionUtils.isNotEmpty(diagnosis)) {
            RecomUserInfoBO userInfo = this.remoter.getUserInfo(doctorId, null, false);
            if (null == userInfo || null == userInfo.getDoctor()) {
                LOGGER.warn("[草稿箱] getDraftInfo error ; 用户信息异常 : doctorId={}", doctorId);
                throw new BusinessException(ExceptionCodes.DOCTOR_RECOM_EXITS);
            }
            RecomDoctorBO doctorBO = userInfo.getDoctor();
            if (doctorBO.getLabel() != null && doctorBO.getLabel().getDiagnosis()) {
                List<String> diagnosisList = new ArrayList<>();
                List<DrStandardDiagnosis> drStandardDiagnoses = this.drStandardDiagnosisRepository.findByNameIn(diagnosis);
                if (CollectionUtils.isNotEmpty(drStandardDiagnoses)) {
                    //获取所有标准诊断，并且移除非标准诊断
                    for (DrStandardDiagnosis tempDiagnosis : drStandardDiagnoses) {
                        diagnosisList.add(tempDiagnosis.getName());
                    }
                    if (drStandardDiagnoses.size() != diagnosis.size()) {
                        draftBoxVO.setDiagnoseDeleteTip(RECOM_DELETE_DIAGNOSE_TIP);
                    }
                } else {
                    draftBoxVO.setDiagnoseDeleteTip(RECOM_DELETE_DIAGNOSE_TIP);
                }
                draftBoxVO.setDiagnoseList(diagnosisList);
            }
        }
        return draftBoxVO;
    }
}
